// homepage.js
var utils = require('../../utils/util.js')
var app = getApp()

function getRandomColor() {
  let rgb = []
  for (let i = 0; i < 3; ++i) {
    let color = Math.floor(Math.random() * 256).toString(16)
    color = color.length == 1 ? '0' + color : color
    rgb.push(color)
  }
  return '#' + rgb.join('')
}

Page({

  inputValue: '',
  /**
   * 页面的初始数据
   */
  data: {
    video:false,
    goodsList:[],
    inputValue:'',//评论值
    damu_btn_send:"",//发送按钮样式
    times:0,//视频播放长度
    danmuList: []
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getGoodsList()
    this.getVideo()
    this.videoContext = wx.createVideoContext('myVideo')
  },

  onLoad: function () {

  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },


  /**
   * 跳转商品详情
   */
  tapDetail: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../detail/detail?id=' + id
    })
  },

  /**
   * 获取视频信息
   */
  getVideo: function () {
    var _this = this
    wx.request({
      url: app.globalData.requestip + "&c=index&a=getVideo",
      success: function (res) {
        var video = res.data
        var danmuList = []
        var len = video.danmu_list.length
        for (var i = 0; i < len; i++) {
          var danmu = {}
          danmu.text = video.danmu_list[i]['content']
          danmu.color = getRandomColor()
          danmu.time = parseInt(video.danmu_list[i]['times'])
          danmuList.push(danmu)
        }
        _this.setData({
          video: video,
          danmuList: danmuList
        })
      },
      fail: function (res) {
        utils.loadRequestFail(res)
      }
    })
  },

  /**
   * 商品列表
   */
  getGoodsList: function () {
    var _this = this
    wx.request({
      url: app.globalData.requestip + "&c=index&a=index",
      success: function (res) {
        var list = res.data
        _this.setData({
          goodsList: list
        })
      },
      fail: function (res) {
        utils.loadRequestFail(res)
      }
    })
  },

  /**
   * 监听输入文字
   */
  bindInput: function (e) {
    this.inputValue = e.detail.value
    if (this.inputValue != null && this.inputValue != "") {
      this.setData({
        damu_btn_send:'damu_btn_send'
      })
    } else {
      this.setData({
        damu_btn_send: ''
      })
    }
  },

  /**
   * 发送弹幕
   */
  bindSendDanmu: function () {
    var _this = this
    if (_this.inputValue != null && _this.inputValue != "") {
      var danmu = {}
      danmu.text = _this.inputValue
      danmu.color = getRandomColor()
      danmu.time = _this.data.times
      _this.videoContext.sendDanmu({
        text: _this.inputValue,
        color: getRandomColor()
      })

      wx.request({
        url: app.globalData.requestip + "&c=Index&a=addDanmu",
        data: {
          vid: _this.data.video.id,
          content: _this.inputValue,
          times: _this.data.times
        },
        header: app.globalData.header,
        success: function (res) {
          if (res.data.code == 0) {
            
          } else {
            utils.loadFail(res)
          }
        },
        fail: function (res) {
          utils.loadRequestFail(res)
        }
      })
      _this.data.danmuList.push(danmu)
      _this.setData({
        inputValue:'',
        damu_btn_send: ''
      })
      _this.inputValue = ''
    }
  },

  bindTimeUpdate:function (e) {
    var times = parseInt(e.detail.currentTime)
    this.data.times = times
  },

  bindPlay() {
    // this.videoContext = wx.createVideoContext('myVideo')
    this.setData({
      danmuList: this.data.danmuList
    })
  }
})
